package com.syg;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


/**
 * springboot静态方法获取 bean 的三种方式(一)
 *
 * @Component 必须交由ioc容器管理
 * @author syg
 * @date 2021年11月4日14:03:07
 * @version 1.0
 */
@Component
public class StaticMethodGetBean01 implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public StaticMethodGetBean01() {
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        StaticMethodGetBean01.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}
